using Autofac.Features.Indexed;
using Cysharp.Threading.Tasks;
using ModuleManager;

namespace ExceptionModule
{
    public class ExceptionStartUp : ModuleStartUpBase
    {
        private readonly IExceptionManager _exceptionManager;
        private readonly ICustomLogHandler _logHandler;
        private readonly IIndex<int, ICustomLogHandler> _customLogHandler;

        public ExceptionStartUp(IExceptionManager exceptionManager, ICustomLogHandler logHandler, IIndex<int, ICustomLogHandler> customLogHandler)
        {
            _exceptionManager = exceptionManager;
            _logHandler = logHandler;
            _customLogHandler = customLogHandler;
        }

        public override UniTask<bool> StartAsync(object context)
        {
            _exceptionManager.Initialize();
            return UniTask.FromResult(true);
        }

        public override UniTask StopAsync()
        {
            _customLogHandler[LogHandleType.LogToConsole]?.Dispose();
            _customLogHandler[LogHandleType.LogToFile]?.Dispose();
            _customLogHandler[LogHandleType.LogToUI]?.Dispose();
            _logHandler?.Dispose();
            _exceptionManager.Quit();
            return base.StopAsync();
        }
    }
}